﻿using ImperialWebSite.Presenters.Specie.Classes;
using ImperialWebSite.Presenters.Specie.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Specie
{
    public partial class QuerySpecies : Page, IQuerySpecies
    {
        private QuerySpeciesPresenter _presenter;
        private bool _successfulTransaction;

        public object SpeciesDataSource
        {
            set { gvSpecies.DataSource = value; }
        }

        public string SpecieNameText
        {
            get { return SpecieName.Text; }
        }


        public object SessionCurrentSpecies
        {
            get { return Session["currentSpecies"]; }
            set { Session["currentSpecies"] = value; }
        }

        public object SessionCurrentSpecie
        {
            get { return Session["currentSpecie"]; }
            set { Session["currentSpecie"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new QuerySpeciesPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitMessages();
                InitComponents();
            }
        }

        private void InitMessages()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void InitComponents()
        {
            SessionCurrentSpecie = null;
            SessionCurrentSpecies = null;
            LoadCurrentSpecies();
            SpecieName.Text = string.Empty;
            //btnRegister.Enabled = SessionCurrentSpecie != null;
        }

        private void LoadCurrentSpecies()
        {
            _presenter.LoadCurrentSpecies();
            gvSpecies.DataBind();
        }


        protected void GvSpecies_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSpecies.DataSource = SessionCurrentSpecies;
            gvSpecies.PageIndex = e.NewPageIndex;
            gvSpecies.DataBind();
            SessionCurrentSpecie = null;
            SpecieName.Text = string.Empty;
        }

        protected void GvSpecies_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            SessionCurrentSpecie = gv.SelectedDataKey["idSpecie"].ToString();
            TransactionMessage = string.Empty;
            SpecieName.Text = gv.SelectedRow.Cells[0].Text;
            btnRegister.Enabled = SessionCurrentSpecie != null;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (SessionCurrentSpecie != null)
            {
                _presenter.UpdateSpecie();
                if (SuccessfulTransaction)
                    InitComponents();
            }
            else
                TransactionMessage = "Please select a Specie";
        }
    }
}